package cn.edu.neusoft.food729.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.retroiftdemo_729.R;

import cn.edu.neusoft.food729.Bean.RegisterBean;
import cn.edu.neusoft.food729.listener.RegisterListener;
import cn.edu.neusoft.food729.model.RegisterModel;


public class RegisterActivity extends AppCompatActivity {
    private EditText etusername, etpass, etqueren, etadress, ettel, etbaizhu;
    private Button btpaizhao, btzhuce, btback;
    private ImageView touxiang;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private RegisterListener registerListener = new RegisterListener() {

        @Override
        public void onResponse(RegisterBean registerBean) {
            System.out.println("---注册结果 ：" + registerBean);
            String s1 = registerBean.getSuccess();
            String s2 = "0";
            if (!s1.equals(s2)) {
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            } else Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFail(String msg) {
            System.out.println("---注册失败：" + msg);
            Toast.makeText(RegisterActivity.this, "注册失败： " + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void initView() {
        etusername = findViewById(R.id.editText3);
        etpass = findViewById(R.id.editText5);
        etqueren = findViewById(R.id.editText6);
        etadress = findViewById(R.id.editText10);
        ettel = findViewById(R.id.editText9);
        btpaizhao = findViewById(R.id.button3);
        btzhuce = findViewById(R.id.button4);
        btback = findViewById(R.id.button5);
        etbaizhu = findViewById(R.id.editText4);

//
//        etpass.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        etqueren.setTransformationMethod(PasswordTransformationMethod.getInstance());

        btpaizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this, "功能待开发", Toast.LENGTH_SHORT).show();
            }
        });

        btzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
                Intent intent_RegisterActivity_to_LoginActivity = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent_RegisterActivity_to_LoginActivity);
                finish();
            }
        });

        btback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                register();
                Intent intent_RegisterActivity_to_LoginActivity = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent_RegisterActivity_to_LoginActivity);
                finish();
            }
        });
    }

    private void register() {
        String stuser = etusername.getText().toString();
        String strpass = etpass.getText().toString();
        String strpass2 = etqueren.getText().toString();
        String staddress = etadress.getText().toString();
        String strtel = ettel.getText().toString();
        String commend = etbaizhu.getText().toString();
        if (TextUtils.isEmpty(stuser)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strpass)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strpass2)) {
            Toast.makeText(this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strtel)) {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!strpass.equals(strpass2)) {
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        RegisterModel registerModel = new RegisterModel();
        registerModel.getRegisterResult(stuser, strpass, strtel, staddress, commend, registerListener);
    }
}
